#ifndef MY_SCENE_H
#define MY_SCENE_H

#include <vector>

#include "models.h"
#include "scene.h"
#include "mycanvas.h"

using namespace std;

class MyScene : public Scene{

    private:
        int vision;

        DrawerTrace drawerTrace;
    public:
        MyScene(){
            this->rotate_x = 0.0;
            this->rotate_y = 0.0;
            this->rotate_z = 0.0;

            this->translate_x = 0.0;
            this->translate_y = 0.0;
            this->translate_z = 0.0;

            this->vision = 0;
            this->ground.setId(900000);
            
            disablePlane();
        }
        ~MyScene(){}
        
        void initTrace(){
            drawerTrace.initTrace();
        }
        
        void addCursor(Cursor &c){
            drawerTrace.addCursor(c);
        }
        
        void setTrace(Trace &t){
            this->drawerTrace.setTrace(t);
        }
        
        
        vector<Selected> parseSelectedBuffer(GLuint selectBuff[SELECT_BUFFER_LENGTH]){
            vector<Selected> selected;
            for (int i = 0; i < SELECT_BUFFER_LENGTH; i+=4){
                if (selectBuff[i] == 1){
                    Selected s;
                    s.count = selectBuff[i];
                    s.zmin = selectBuff[i+1];
                    s.zmax = selectBuff[i+2];
                    s.id = selectBuff[i+3];
                    selected.push_back(s);
                }
                else{
                    break;
                }
            }            
            return selected;
        }

        void updateEnviroment(){
            if((this->getRotateY() > 45 and this->getRotateY() < 135) 
            or (this->getRotateY() > -135 and this->getRotateY() < -45)){
                this->vision = 2;
            }
            else{
                this->vision = 1;
            }
            if(this->getRotateX() > -45){
                disablePlane();
                this->vision = 0;
            }         
        }
    
       
        void drawTrace(){
            this->drawerTrace.draw();
        }
        
        void setUp(){
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glClearColor(0.80f, 0.80f, 0.80f, 1.0f );            
            glColor3f(1.0f, 0.0f, 0.0f);
            
            glMatrixMode(GL_MODELVIEW); 
            glLoadIdentity(); 

            glTranslatef(translate_x, translate_y, translate_z);
            
            glRotatef(rotate_x, 1.0, 0.0, 0.0);
            glRotatef(rotate_y, 0.0, 1.0, 0.0);   
            glRotatef(rotate_z, 0.0, 0.0, 1.0);          
            
            glInitNames();
            glPushName(0);
        }        
};

#endif /* MY_SCENE_H */
